package com.company.reptile;


import com.company.FoundationHuobi;
import com.sun.istack.internal.Nullable;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;

/*描述 网络爬虫  多线程 货币基金排行
 * @authro szq
 * @time 2022/01/05*/
public class ReptilerFoundationHuobiPaiHangForTelUtil {


    private static Logger log = Logger.getLogger(ReptilerFoundationHuobiPaiHangForTelUtil.class);
    private static Vector<FoundationHuobi> foundationHuobiList;
    private static Object xlock = new Object();
    private static String mtype;


    public static void main(String[] args) throws InterruptedException, FileNotFoundException {
        Reptler("a");
    }

    public static Vector<FoundationHuobi> Reptler(@Nullable String type) throws InterruptedException, FileNotFoundException {
        //        ------------------------------------

        foundationHuobiList = null;
        xlock = new Object();

        //        ------------------------------------
        foundationHuobiList = new Vector<>();

//        type: a：A类 b：B类 空：全部
        String urlPath = "http://api.fund.eastmoney.com/FundRank/GetHbRankList?" +
                "intCompany=0&MinsgType=" + type
                + "&IsSale=0&strSortCol=SYL_1N&orderType=desc&pageIndex=1&pageSize=10000&callback=jQuery18306549942710882048_1678775213655&_=1678775248297";
        String msg = getHtml(urlPath);
        setHtml(msg);
        System.out.println("货币基金:" + mtype + "共爬取" + foundationHuobiList.size() + "条记录！");

        return foundationHuobiList;

    }

    public static String getHtml(String urlPath) {

        String msg = "";

        HttpURLConnection connection = null;
        try {
            // 1. 得到访问地址的URL
            URL url = new URL(urlPath);
            // 2. 得到网络访问对象java.net.HttpURLConnection
            connection = (HttpURLConnection) url.openConnection();
//             伪装浏览器
//            connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0");
            connection.setRequestProperty("charset", "utf-8"); //设置请求编码
            connection.setRequestProperty("Cookie", "st_si=12097774306782; st_sn=7; st_psi=20200712090801308-0-3225966371; st_asi=delete; ASP.NET_SessionId=d3i2hxkiu0rzmshtsa3kw514; st_pvi=49218209189172; st_sp=2020-07-11%2012%3A21%3A10; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink");
            connection.setRequestProperty("Referer", "http://fund.eastmoney.com/data/fundranking.html");

            /* 3. 设置请求参数（过期时间，输入、输出流、访问方式），以流的形式进行连接 */
            // 设置是否向HttpURLConnection输出
            connection.setDoOutput(false);
            // 设置是否从httpUrlConnection读入
            connection.setDoInput(true);
            // 设置请求方式　默认为GET
            connection.setRequestMethod("GET");
            // 设置是否使用缓存
            connection.setUseCaches(true);
            // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
            connection.setInstanceFollowRedirects(true);
            // 设置超时时间
            connection.setConnectTimeout(3000);
            // 连接
            connection.connect();
            // 4. 得到响应状态码的返回值 responseCode
            int code = connection.getResponseCode();
            // 5. 如果返回值正常，数据在网络中是以流的形式得到服务端返回的数据

            if (code == 200) { // 正常响应
                // 从流中读取响应信息
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = reader.readLine()) != null) { // 循环从流中读取
                    msg += line;
                }
                /////////////////
//                InputStream input = connection.getInputStream();
////                    StringBuffer sb1= new StringBuffer();
//                int len = 0;
//                byte[] buffer = new byte[1024];
//                StringBuilder builder = new StringBuilder();
//                while ((len = input.read(buffer)) != -1) {
////                        sb1.append((char)ss);
//                    builder.append(new String(buffer, 0, len, "GBK"));
//                }
//                msg = builder.toString();
                /////////////////
                reader.close(); // 关闭流
            }
//            // 显示响应结果
//            log.info(msg);
        } catch (IOException e) {
            log.error("错误信息：" + e.getLocalizedMessage() + ";" + e.getClass());
        } finally {
            // 6. 断开连接，释放资源
            if (null != connection) {
                try {
                    connection.disconnect();
                } catch (Exception e) {
                    log.info("connection 流关闭异常：" + e.getLocalizedMessage());
                }
            }
        }
        return msg;
    }


    /*爬取网页源代码并处理
     * @param requestUrl String 请求地址
     * @return */
    public static void setHtml(String msg) {
        msg = msg.replace("\n", "")
                .replace("\t", "")
                .replace(";", "")
                .replace("jQuery18306549942710882048_1678775213655(", "")
                .replace(")", "");
        JSONObject jsonObject = JSONObject.fromObject(msg);
        JSONArray jsonArray = JSONArray.fromObject(jsonObject.get("Data"));
        if (jsonArray != null && jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject2 = JSONObject.fromObject(jsonArray.get(i));
                FoundationHuobi foundationHuobi = new FoundationHuobi();
                foundationHuobi.setSYL_1N(jsonObject2.get("SYL_1N").toString());
                foundationHuobi.setSYL_2N(jsonObject2.get("SYL_2N").toString());
                foundationHuobi.setSYL_3N(jsonObject2.get("SYL_3N").toString());
                foundationHuobi.setSYL_5N(jsonObject2.get("SYL_5N").toString());
                foundationHuobi.setFCODE(jsonObject2.get("FCODE").toString());
                foundationHuobi.setSHORTNAME(jsonObject2.get("SHORTNAME").toString());
                foundationHuobi.setFSRQ(jsonObject2.get("FSRQ").toString());
                foundationHuobi.setDWJZ(jsonObject2.get("DWJZ").toString());
                foundationHuobi.setLJJZ(jsonObject2.get("LJJZ").toString());
                foundationHuobi.setJZ(jsonObject2.get("JZ").toString());
                foundationHuobi.setFTYI(jsonObject2.get("FTYI").toString());
                foundationHuobi.setTEYI(jsonObject2.get("TEYI").toString());
                foundationHuobi.setSYL_Y(jsonObject2.get("SYL_Y").toString());
                foundationHuobi.setSYL_3Y(jsonObject2.get("SYL_3Y").toString());
                foundationHuobi.setSYL_6Y(jsonObject2.get("SYL_6Y").toString());
                foundationHuobi.setSYL_JN(jsonObject2.get("SYL_JN").toString());
                foundationHuobi.setSYL_LN(jsonObject2.get("SYL_LN").toString());
                foundationHuobi.setISBUY(jsonObject2.get("ISBUY").toString());
                foundationHuobi.setRATE(jsonObject2.get("RATE").toString());
                foundationHuobi.setFEATURE(jsonObject2.get("FEATURE").toString());
                foundationHuobi.setBFUNDTYPE(jsonObject2.get("BFUNDTYPE").toString());
                foundationHuobi.setABBNAME(jsonObject2.get("ABBNAME").toString());
                foundationHuobi.setSOURCERATE(jsonObject2.get("SOURCERATE").toString());
                foundationHuobi.setDISCOUNT(jsonObject2.get("DISCOUNT").toString());
                foundationHuobi.setFTYPE(jsonObject2.get("FTYPE").toString());
                foundationHuobiList.add(foundationHuobi);
            }
            System.out.println(foundationHuobiList.toString());
        }
    }


}